<template>
  <div>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList ()">查询</el-button>
        <el-button v-if="isAuth('auth:coreorg:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-dropdown-item><el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button></el-dropdown-item>
        <el-dropdown split-button type="primary">
          更多
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            </el-dropdown-item>
            <el-button v-if="isAuth('auth:coreorg:delete')" type="danger" @click="deleteHandle()"
                       :disabled="dataListSelections.length <= 0">批量删除
            </el-button>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>

    </el-form>
    <div>
    <el-tree
        :data="data"
        show-checkbox
        :default-expand-all="flag"
        node-key="id"
        ref="tree"
        highlight-current
        :props="defaultProps"
    >
    </el-tree>
    </div>

        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import {getcoreorgList} from '@/api/coreorg.js'
import AddOrUpdate from "@/views/modules/coreorg/coreorg-add-or-update";

export default {
  data() {
    return {
      flag: true,
      data: [],
      expandedKeys: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dataForm: {
        key: ''
      },

      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    AddOrUpdate
  },

  mounted() {
    this.getMenus();
  },
  methods: {
    getMenus() {
      getcoreorgList().then(({data}) => {
        this.data = data.page;
        console.log("成功获取数据...", data.page);
      });
    },
    getDataList() {
      let params = {
        'page': this.pageIndex,
        'limit': this.pageSize,
        'key': this.dataForm.key
      }
      getcoreorgList(params).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/auth/coreorg/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  },
};
</script>
